package com.library.management.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.management.dto.BookDTO;
import com.library.management.dto.BookQueryParam;
import com.library.management.service.BooksService;
import com.library.management.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 图书控制器
 */
@Api(tags = "图书管理接口")
@RestController
@RequestMapping("/api/books")
public class BooksController {
    
    @Autowired
    private BooksService booksService;
    
    /**
     * 获取图书列表（分页）
     * @param queryParam 查询参数
     * @return 图书列表
     */
    @ApiOperation(value = "获取图书列表", notes = "分页查询图书列表，支持关键词搜索")
    @GetMapping
    @SaCheckLogin
    public Result<Map<String, Object>> getBookList(BookQueryParam queryParam) {
        IPage<BookDTO> page = booksService.getBookPage(queryParam);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", page.getTotal());
        result.put("pages", page.getPages());
        result.put("list", page.getRecords());
        
        return Result.success(result);
    }
    
    /**
     * 获取图书详情
     * @param id 图书ID
     * @return 图书详情
     */
    @ApiOperation(value = "获取图书详情", notes = "根据ID获取图书详细信息")
    @GetMapping("/{id}")
    @SaCheckLogin
    public Result<BookDTO> getBookDetail(
            @ApiParam(value = "图书ID", required = true) @PathVariable Integer id) {
        BookDTO bookDTO = booksService.getBookById(id);
        return Result.success(bookDTO);
    }
    
    /**
     * 添加图书
     * @param bookDTO 图书信息
     * @return 添加结果
     */
    @ApiOperation(value = "添加图书", notes = "添加新图书（需要管理员权限）")
    @PostMapping
    @SaCheckRole("admin")
    public Result<Integer> addBook(@Validated @RequestBody BookDTO bookDTO) {
        Integer bookId = booksService.addBook(bookDTO);
        return Result.success(bookId, "添加图书成功");
    }
    
    /**
     * 更新图书
     * @param id 图书ID
     * @param bookDTO 图书信息
     * @return 更新结果
     */
    @ApiOperation(value = "更新图书", notes = "更新图书信息（需要管理员权限）")
    @PutMapping("/{id}")
    @SaCheckRole("admin")
    public Result<Boolean> updateBook(
            @ApiParam(value = "图书ID", required = true) @PathVariable Integer id,
            @Validated @RequestBody BookDTO bookDTO) {
        boolean success = booksService.updateBook(id, bookDTO);
        return Result.success(success, "更新图书成功");
    }
    
    /**
     * 删除图书
     * @param id 图书ID
     * @return 删除结果
     */
    @ApiOperation(value = "删除图书", notes = "删除图书（需要管理员权限）")
    @DeleteMapping("/{id}")
    @SaCheckRole("admin")
    public Result<Boolean> deleteBook(
            @ApiParam(value = "图书ID", required = true) @PathVariable Integer id) {
        boolean success = booksService.deleteBook(id);
        return Result.success(success, "删除图书成功");
    }
} 